<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_退回件质检</title>
<script type="text/javascript" src="${ctx}/static/v2/js/modal.js"></script>
<script type="text/javascript">

  $(function() {
       //如果不在质检状态，隐藏操作按钮
	   var status='${eOrder.backStatus}';
	   if(status!='receive'){ 
	      $("button").hide();   

	   }
	   
	   $("#txt_sku_1").val('');
	   $("#txt_sku_2").val('');
	   $("#txt_sku_1").focus();
	   
  });
  
  function keydown(){
	  $("#msg_1").html("");
	  $("#msg_2").html("");
  }
  
  //add 20160620
  function numConfirm(errorFlag, e) {
	  if (e.keyCode != 13) {
		  return;
	  }
	  
	  if('error'==errorFlag){
        var sku=$("#txt_sku_2").val().trim();
        var batchCode=$("#bad_txt_batchCode").val().trim();
        var productDate=$("#bad_txt_productDate").val().trim();
        var expireDate=$("#bad_txt_expireDate").val().trim();
	    var num = $("#txt_num_2").val().trim();
      }
      else if('noterror'==errorFlag){
        var sku=$("#txt_sku_1").val().trim();
        var batchCode=$("#good_txt_batchCode").val().trim();
        var productDate=$("#good_txt_productDate").val().trim();
        var expireDate=$("#good_txt_expireDate").val().trim();
	    var num = $("#txt_num_1").val().trim();
      }
	  
	  if(sku ==''){
		alert("SKU为空！");
	  	return;
	  }
	  
	 if (num == '') {
		 alert("数量为空！");
		 return;
	 } else if (num <= 0) {
		 alert("数量不正确！");
		 return;
	 }
		  
      var exceptionId='${eOrder.id}';
	  
      $.ajax({
			type:'post',
			url:'${ctx}/v2/backOrder/ajaxQc',
			data:{
				exceptionId:exceptionId,
				sku:sku,
				batchCode:batchCode,
				productDate:productDate,
				expireDate:expireDate,
				errorFlag:errorFlag,
				num:num
			},
			dataType:'json',
			success:function(data) {
				var msg=data.msg;
				
			    if( msg=='success'){
			       var okList=data.okList;
			       var errorList=data.errorList;
			       
			       //完好件
			       $("#qcItem_ok").html('');
			       var str='';
			       $.each(okList,function(i,item){
			    	   str+="<tr id='tr_"+item.id+"'>";
			    	   str+="<td><input  type='text' id='sku1_"+item.id+"' value='"+item.sku+"'/></td>";
			    	   str+="<td><input  type='text' id='num1_"+item.id+"' value='"+item.num+"'/></td>";
			    	   str+="<td><input  type='text' id='batch1_"+item.id+"' value='"+item.batchCode+"'/></td>";
			    	   str+="<td><input  type='text' id='product1_"+item.id+"' value='"+item.productDate+"'/></td>";
			    	   str+="<td><input  type='text' id='expire1_"+item.id+"' value='"+item.expireDate+"'/></td>";
			    	   str+="<td><button class='btn btn-danger' onclick='deleteItem("+item.id+");'>删除</button></td>";
			    	   str+="</tr>";
			       });
			       $("#qcItem_ok").html(str);
			       
			       //报损件
			       $("#qcItem_error").html('');
			       var str='';
			       $.each(errorList,function(i,item){
			    	   str+="<tr id='tr_"+item.id+"'>";
			    	   str+="<td><input  type='text' id='sku2_"+item.id+"' value='"+item.sku+"'/></td>";
			    	   str+="<td><input  type='text' id='num2_"+item.id+"' value='"+item.num+"'/></td>";
			    	   str+="<td><input  type='text' id='batch2_"+item.id+"' value='"+item.batchCode+"'/></td>";
			    	   str+="<td><input  type='text' id='product2_"+item.id+"' value='"+item.productDate+"'/></td>";
			    	   str+="<td><input  type='text' id='expire2_"+item.id+"' value='"+item.expireDate+"'/></td>";
			    	   str+="<td><button class='btn btn-danger' onclick='deleteItem("+item.id+");'>删除</button></td>";
			    	   str+="</tr>";
			       });
			       $("#qcItem_error").html(str);
			       
			       if("error"==errorFlag){
			         $("#txt_sku_2").focus();
			         $("#msg_2").css("color","green");
				     $("#msg_2").html("点收成功。");
			       }
			       else if("noterror"==errorFlag){
			         $("#txt_sku_1").focus();
			         $("#msg_1").css("color","green");
				     $("#msg_1").html("点收成功。");
			       }
			       
			       $("#txt_sku_1").val('');	
			       $("#txt_sku_2").val('');
			       $("#good_txt_batchCode").val('');
			       $("#good_txt_productDate").val('');
			       $("#good_txt_expireDate").val('');
			       $("#bad_txt_batchCode").val('');
			       $("#bad_txt_productDate").val('');
			       $("#bad_txt_expireDate").val('');
			       
			       //add 20160620
				   $("#txt_num_1").val('');
				   $("#txt_num_2").val('');
				  
			    }else if(msg=='notmatch'){
			      alert("sku与发货时不匹配,请确认");
			      //add 20160620
			      if("error"==errorFlag){
			    	  $("#txt_sku_2").focus();
			      } else if("noterror"==errorFlag){
			    	  $("#txt_sku_1").focus();
			      }
			    } else if (msg == 'error_num') {
			    	alert("请输入大于0的正整数!");
			    } else {
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  }
  
  	//modify 20160620
  	function skuChange(errorFlag, e){
	  	var exceptionId='${eOrder.id}';
	  	if (e.keyCode != 13) {
		  	return;
	  	}
	  	else {
	  		var sku;
		  	var num;
	      	if('error'==errorFlag){
		        sku=$("#txt_sku_2").val();
		        num = $("#txt_num_2").val();
	      	} else if('noterror'==errorFlag){
		        sku=$("#txt_sku_1").val();
		        num = $("#txt_num_1").val();
	      	}
	      	$.ajax({
				type:'post',
				url:'${ctx}/v2/backOrder/checkGoods',
				data:{
					exceptionId:exceptionId,
					sku:sku
				},
				dataType:'json',
				success:function(data) {
					if(data.status == "success"){
						if(data.isShelfLifeMgmt != 0 || data.isBatchMgmt != 0){
							var batchList, batchCode_modal, productDate_modal, expireDate_modal, model_data;
							batch(data.isShelfLifeMgmt, data.isBatchMgmt, data.batchList ,function(batchList){
								batchCode_modal = batchList[0].batch_input == undefined ? '' : batchList[0].batch_input;
								productDate_modal = batchList[1].batch_start == undefined ? '' : batchList[1].batch_start;
								expireDate_modal = batchList[2].batch_end == undefined ? '' : batchList[2].batch_end;
								//破损件
								if('error'==errorFlag){
							        if (data.isBatchMgmt != 0) {
										$("#bad_txt_batchCode").val(batchCode_modal);
										$('.bad_txt_batchCode').css('display','inline-block');
									}
									if (data.isShelfLifeMgmt != 0) {
										$("#bad_txt_productDate").val(productDate_modal);
										$("#bad_txt_expireDate").val(expireDate_modal);
										$('.bad_txt_expireDate').css('display','inline-block');
									}
									$('#txt_num_2').focus();
							    } else if('noterror'==errorFlag){
							        if (data.isBatchMgmt != 0) {
										$("#good_txt_batchCode").val(batchCode_modal);
										$('.good_txt_batchCode').css('display','inline-block');
									}
									if (data.isShelfLifeMgmt != 0) {
										$("#good_txt_productDate").val(productDate_modal);
										$("#good_txt_expireDate").val(expireDate_modal);
										$('.good_txt_expireDate').css('display','inline-block');
									}
									$('#txt_num_1').focus();
							    }
							});
						}else{
							if('error'==errorFlag){
								$('#txt_num_2').focus();
						    } else if('noterror'==errorFlag){
								$('#txt_num_1').focus();
						    }
						}
				    }else if(data.status == "skuNotExist"){
				       alert(data.content);
				    }else {
				      alert("网络繁忙，请稍后重试...");
				    }
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
		   	});
		    /*if(sku.val().trim() ==''){
			  	return;
			} else {
				num.val('1');
				num.focus();
			}*/
	  	}
	  	
  	}
  
  //删除
  	function deleteItem(itemId){
	  	$.ajax({
			type:'post',
			url:'${ctx}/v2/backOrder/ajaxDeleteQcItem',
			data:{
				itemId:itemId
			},
			dataType:'json',
			success:function(data) {
			    if( data==true){
				   $("#tr_"+itemId).remove();
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  	}
  
  //完成点收
  function finish(){
     
      var btn=$("#btn_finish"); 
      btn.attr("disabled",true);
      btn.attr("class","btn");
  
	  $.ajax({
			type:'post',
			url:'${ctx}/v2/backOrder/ajaxFinishQc',
			data:{
				exceptionId:'${eOrder.id}'
			},
			dataType:'json',
			success:function(data) {
			    if(data.msg=="success"){
			    	window.location.href = "${ctx}/v2/backOrder/show";
			    }else if(data.msg=="notMatch"){
			      $("#msg_1").html("点收SKU数量和拣货数量不一致,请核对.");
			    }else{
			      alert("网络繁忙，请稍后重试...");
			      btn.attr("disabled",false);
                  btn.attr("class","btn btn-primary");
			    }
			},
			error:function() {
				jAlert("网络繁忙，请稍后重试...");
				btn.attr("disabled",false);
                btn.attr("class","btn btn-primary");
			}
	   });
  }

</script>
</head>

<body>
<section class="content">
<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">质检</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				
	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
<!-- 			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#8EB4CB">质检</th> -->
		</thead>
		<tr>
		    <td style="vertical-align: middle;text-align: center;width: 150px;">完好件扫描</td>
		    <td > 
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_sku_1" type="text" onkeydown="skuChange('noterror', event);"/>
		       <div style="display:none;" class="good_txt_batchCode">
		       		<span style="margin-left: 10px;">批次号:</span>
		       		<input id="good_txt_batchCode" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="good_txt_expireDate">
		       		<span style="margin-left: 10px;">生产日期:</span>
		       		<input id="good_txt_productDate" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="good_txt_expireDate">
		       		<span style="margin-left: 10px;">过期日期:</span>
		       		<input id="good_txt_expireDate" type="text" disabled/>
		       	</div>
		       <span style="margin-left: 10px;">数量:</span>
		       <input id="txt_num_1" type="text" onkeydown="numConfirm('noterror', event);"/>
		       <span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg_1"></span>
		    </td>
		</tr>
		<tr>
		    <td style="vertical-align: middle;text-align: center;">报损件扫描</td>
		    <td> 
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_sku_2" type="text" onkeydown="skuChange('error', event);"/>
		       <div style="display:none;" class="bad_txt_batchCode">
		       		<span style="margin-left: 10px;">批次号:</span>
		       		<input id="bad_txt_batchCode" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="bad_txt_expireDate">
		       		<span style="margin-left: 10px;">生产日期:</span>
		       		<input id="bad_txt_productDate" type="text" disabled/>
		       	</div>
		       	<div style="display:none;" class="bad_txt_expireDate">
		       		<span style="margin-left: 10px;">过期日期:</span>
		       		<input id="bad_txt_expireDate" type="text" disabled/>
		       	</div>
		       <span style="margin-left: 10px;">数量:</span>
		       <input id="txt_num_2" type="text" onkeydown="numConfirm('error', event);"/>
		       <span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg_2"></span>
		    </td>
		</tr>
		<tr>
		    <td style="vertical-align: middle;text-align: center;">操作</td>
		    <td>
                <button class="btn btn-primary" id="btn_finish" onclick="finish('${eOrder.id}');">完成质检</button>		      
		    </td>
		</tr>
	</table>
	</div></div></div></div>
	
	
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">完好件数据</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				
	<!-- 完好件数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
<!-- 			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#8EB4CB">完好件数据</th> -->
		</thead>
		<thead><tr>
			    <th style="width: 315px;">SKU</th>
			    <th style="width: 315px;">数量</th>
			    <th style="width: 315px;">批次号</th>
			    <th style="width: 315px;">生产日期</th>
			    <th style="width: 315px;">过期日期</th>
			    <th>操作</th>
			</tr></thead>
		<tbody id="qcItem_ok">
		<c:forEach items="${okList}" var="t" varStatus="i">
			<tr id="tr_${t.id}">
			   <td><input type="text" id="sku1_${t.id}" value="${t.sku}"/></td>
			   <td><input type="text" id="num1_${t.id}" value="${t.num}"/></td>
			   <td><input type="text" id="batch1_${t.id}" value="${t.batchCode}"/></td>
			   <td><input type="text" id="product1_${t.id}" value="${t.productDate}"/></td>
			   <td><input type="text" id="expire1_${t.id}" value="${t.expireDate}"/></td>
			   <td><button class="btn btn-danger" onclick="deleteItem('${t.id}');">删除</button></td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	</div></div></div></div>
	
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">报损件数据</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				
	<!-- 报损件数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
<!-- 			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#8EB4CB">报损件数据</th> -->
		</thead>
		<thead><tr>
		    <th style="width: 315px;">SKU</th>
		    <th style="width: 315px;">数量</th>
		    <th style="width: 315px;">批次号</th>
		    <th style="width: 315px;">生产日期</th>
		    <th style="width: 315px;">过期日期</th>
		    <th>操作</th>
		</tr></thead>
		<tbody id="qcItem_error">
		<c:forEach items="${errorList}" var="t" varStatus="i">
			<tr id="tr_${t.id}">
			   <td><input type="text" id="sku2_${t.id}" value="${t.sku}"/></td>
			   <td><input type="text" id="num2_${t.id}" value="${t.num}"/></td>
			   <td><input type="text" id="batch2_${t.id}" value="${t.batchCode}"/></td>
			   <td><input type="text" id="product2_${t.id}" value="${t.productDate}"/></td>
			   <td><input type="text" id="expire2_${t.id}" value="${t.expireDate}"/></td>
			   <td><button class="btn btn-danger" onclick="deleteItem('${t.id}');">删除</button></td>
			</tr>
		</c:forEach>
		</tbody>
		
	</table>
	
	</div></div></div></div>
</section>

</body>
</html>
